﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Data.SQLite;

namespace LuZhouLaoJiao
{
    public partial class DeleteUser : Form
    {
        public DeleteUser()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (Global.userName == tbUserName.Text.ToString().Trim())
            {
                MessageBox.Show("不能删除自身");
                return;
            }

            DialogResult result = MessageBox.Show("确定删除账户" + tbUserName.Text.ToString() + "？", "删除账户", MessageBoxButtons.YesNo);

            if (result == DialogResult.Yes)
            {
                //删除
                //Global.dbUser = Global.getRegeditValue("localDBUser");
                //Global.dbPwd = Global.getRegeditValue("localDBPwd");
                //Global.database = Global.getRegeditValue("localDatabase");
                //Global.localDBIP = Global.getRegeditValue("localIP");
                //string strCon = "server = " + Global.localDBIP + "; user id = " + Global.dbUser + "; password = " + Global.dbPwd + "; database = " + Global.database + "; persist security info = false";
                //MySqlConnection conn = new MySqlConnection(strCon);
                //MySqlCommand comm = new MySqlCommand();

                SQLiteConnection conn = new SQLiteConnection("Data Source=./data.db");
                SQLiteCommand comm = new SQLiteCommand();
                //MessageBox.Show("sqlite");
                conn.Open();
                comm = conn.CreateCommand();


                comm.CommandText = "select count(*) from batch_info;";
                comm.Connection = conn;
                comm.CommandText = "select count(*) from user where name='" + tbUserName.Text.ToString().Trim() + "';";
                try
                {
                    int i = Convert.ToInt32(comm.ExecuteScalar());
                    if (i > 0)
                    {
                        comm.CommandText = "delete from user where name = '"+tbUserName.Text.ToString().Trim()+"';";
                        int row = comm.ExecuteNonQuery();
                        if (row > 0)
                        {
                            MessageBox.Show("账户" + tbUserName.Text.ToString().Trim() + "已经被删除.");
                        }
                        else
                        {
                            MessageBox.Show("账户" + tbUserName.Text.ToString().Trim() + "未能成功删除.");
                        }
                    }
                    else
                    {
                        MessageBox.Show("账户" + tbUserName.Text.ToString().Trim() + "不存在！");
                    }
                    
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("删除账户出现错误(DU)：" + ex.Message);
                    
                }
                finally
                {
                    conn.Close();
                }
            }
            else
            {
                //不删除
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
